package com.atguigu.lease.web.admin.custom.convert;

import com.atguigu.lease.model.enums.BaseEnum;
import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.stereotype.Component;

/**
 * @ClassName StringToBaseEnumConverterFactory
 * @Author heartFade
 * @Date 2025-04-24 18:54
 * @Version 1.0
 * @Description 实现所有的Item的枚举类的转化
 **/
@Component
public class StringToBaseEnumConverterFactory implements ConverterFactory<String, BaseEnum> {
    @Override
    public <T extends BaseEnum> Converter<String, T> getConverter(Class<T> targetType) {
     return source->{
         T[] enumConstants = targetType.getEnumConstants();
         if (enumConstants != null)
         {
             for(BaseEnum itemType:enumConstants)
             {
                 if(String.valueOf(itemType.getCode()).equals(source))
                     return (T) itemType;
             }
         }
return null;
     };
    }
}
